﻿using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace DTcms.Web.Mvc.UI
{
    public class ModelStateFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 添加统一的ModelState验证
        /// </summary>
        /// <param name="actionContext">actionContext</param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.ModelState.IsValid == false)
            {
                // 此处可以加自定义格式处理
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, actionContext.ModelState.Values, "application/json");
            }
        }
    }
}